package cn.hahah.module.blog.service.userreply;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.hahah.module.blog.controller.admin.userreply.vo.*;
import cn.hahah.module.blog.dal.dataobject.userreply.UserReplyDO;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.util.object.BeanUtils;

import cn.hahah.module.blog.dal.mysql.userreply.UserReplyMapper;

import static cn.hahah.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.hahah.module.blog.enums.ErrorCodeConstants.*;

/**
 * 用户回复评论 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserReplyServiceImpl implements UserReplyService {

    @Resource
    private UserReplyMapper userReplyMapper;

    @Override
    public Long createUserReply(UserReplySaveReqVO createReqVO) {
        // 插入
        UserReplyDO userReply = BeanUtils.toBean(createReqVO, UserReplyDO.class);
        userReplyMapper.insert(userReply);
        // 返回
        return userReply.getId();
    }

    @Override
    public void updateUserReply(UserReplySaveReqVO updateReqVO) {
        // 校验存在
        validateUserReplyExists(updateReqVO.getId());
        // 更新
        UserReplyDO updateObj = BeanUtils.toBean(updateReqVO, UserReplyDO.class);
        userReplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserReply(Long id) {
        // 校验存在
        validateUserReplyExists(id);
        // 删除
        userReplyMapper.deleteById(id);
    }

    private void validateUserReplyExists(Long id) {
        if (userReplyMapper.selectById(id) == null) {
            throw exception(USER_REPLY_NOT_EXISTS);
        }
    }

    @Override
    public UserReplyDO getUserReply(Long id) {
        return userReplyMapper.selectById(id);
    }

    @Override
    public PageResult<UserReplyDO> getUserReplyPage(UserReplyPageReqVO pageReqVO) {
        return userReplyMapper.selectPage(pageReqVO);
    }

}